package com.example.a26436.newpractise.practise2;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a26436.newpractise.R;
import com.example.a26436.newpractise.webview.ToastUtil;
import com.example.a26436.newpractise.webview.WebVIiewActivity;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 26436 on 2020/9/19.
 */

public class Main_pageFragment extends Fragment {

    private ViewPager vpbanner;
    private RecyclerView rvarticle;
    private static final String TAG = "MainPageFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_page, null, false);
        vpbanner = view.findViewById(R.id.vp_banner_fragment_main_page);
        rvarticle = view.findViewById(R.id.rv_article_fragment_main_page);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestBanner();
        requestarticle();
    }



    private void requestarticle() {
        OkHttpClient client = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        builder.url("https://www.wanandroid.com/article/list/0/json");
        Request request = builder.build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                ArticleEnity articleEnity = new Gson().fromJson(result,ArticleEnity.class);
                if(articleEnity.getErrorCode() == 0){
                    final List<ArticleEnity.DataBean.DatasBean> datas = articleEnity.getData().getDatas();
                    HanderUtil.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                            rvarticle.setLayoutManager(linearLayoutManager);
                            ArticleAdapter articleAdapter = new ArticleAdapter(datas, getContext());
                            articleAdapter.setOnArticleItemClickListener(new ArticleAdapter.onArticleItemClickListener() {
                                @Override
                                public void onArticleItemClick(
                                        int position,
                                        ArticleEnity.DataBean.DatasBean datasBean) {
                                    ToastUtil.showToast(getContext(),datasBean.getLink());
                                    Intent intent = new Intent(getContext(), WebVIiewActivity.class);
                                    intent.putExtra(WebVIiewActivity.URL,datasBean.getLink());
                                    startActivity(intent);
                                }
                            });
                            rvarticle.setAdapter(articleAdapter);

                        }
                    });


                }

            }
        });

    }
    private void requestBanner(){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        builder.url("https://www.wanandroid.com/banner/json");
        Request request = builder.build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG,"OnResponse:"+ result);
                Gson gson = new Gson();
                final BannerEnity bannerEnity = gson.fromJson(result, BannerEnity.class);
                if(bannerEnity.getErrorCode()==0){
                 Log.e(TAG,"OnResponse:" +0);
                   HanderUtil.post(new Runnable() {
                       @Override
                       public void run() {
                           BannerAdapter bannerAdapter = new BannerAdapter(bannerEnity.getData(), getContext());
                           vpbanner.setAdapter(bannerAdapter);
                       }
                   });

                }
            }
        });

    }
}
